package com.xjt.sentinel.controller.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.xjt.sentinel.controller.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xu
 * @Description: TODO
 * @Date: 2021/11/14 14:09
 */
@RestController
public class SentinelController
{
	@GetMapping("/hello")
	public String helloWorld()
	{
		Entry entry = null;
		try
		{
//			entry = SphU.entry("hello");
			return "hello sentinel "+System.currentTimeMillis();
		}catch (Exception e){
			return "服务出错了，请稍后重试";
		}
	}


	@GetMapping("/user/{id}")
	@SentinelResource(value = "user",blockHandler = "blockHandleSentinel",fallback = "fallBackTest")
	public User getUser(@PathVariable("id") int id)
	{
		//当为0得时候服务发送异常进入熔断
		int i = 10/id;
		return new User("xujiangtao","testPasswod");
	}

	public User blockHandleSentinel(@PathVariable("id") int id,BlockException throwable)
	{
		System.out.println(throwable);
		return new User("flow impose ,please try again later,params is "+id,"no passwword");
	}
	public User fallBackTest(int id)
	{
		return new User("method is exception,please try again later,params is "+id,
				"this is use service fusing");
	}
}
